<?php

namespace App\Console\Commands;

use App\Models\PayFee;
use Illuminate\Console\Command;
use Carbon\Carbon;
use App\Models\ParkInfo;
use App\Models\Report;

class IncomeCountDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'income:count_day';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '收入日报表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start = Carbon::yesterday()->timestamp;

        $end = Carbon::today()->timestamp;

        $m = 4;
        ini_set('memory_limit', '250M');
        for($i=1; $i<=30;$i++) {
            if ($i < 10) {
                $start = strtotime("2018-0$m-0" . $i . " 00:00:00");
                if (($i + 1) < 10) {
                    $end = strtotime("2018-0$m-0" . ($i + 1) . " 00:00:00");
                } else {
                    $end = strtotime("2018-0$m-" . ($i + 1) . " 00:00:00");
                }
            } else {
                $start = strtotime("2018-0$m-" . $i . " 00:00:00");
                if (($i + 1) <= 30) {
                    $end = strtotime("2018-0$m-" . ($i + 1) . " 00:00:00");
                } else {
                    $end = strtotime("2018-05-01 00:00:00");
                }
            }

            $parks = ParkInfo::whereIn('park_code', config('custom.report_parks'))
                ->select(['park_code', 'parking_name'])
                ->orderBy('sort_by', 'desc')
                ->get()
                ->pluck('park_code');

            //查询收入
            $res = PayFee::groupBy(['park_code', 'pay_type'])
                ->where('pay_time', '>=', $start)
                ->where('pay_time', '<', $end)
                ->select([
                    \DB::raw('IFNULL(SUM(pay_amount),0) as pay_amount'),
                    \DB::raw('sum(if(pay_amount>0,1,0)) as pay_number'),
                    'park_code',
                    'pay_type'
                ])
                ->whereIn('park_code', $parks)
                ->get()
                ->toArray();

            $newArray = [];

            foreach ($res as $k => $value) {
                $valueKey = 'charge_' . $value['pay_type'] . '_' . $value['park_code'];
                $numberKey = 'number_' . $value['pay_type'] . '_' . $value['park_code'];
                $newArray[$valueKey] = $value['pay_amount'] / 100;
                $newArray[$numberKey] = $value['pay_number'];
            }

            //存入数据库
            $report = new Report();
            $report->report_date = date("Y-m-d",$start);
            $report->type = 'income_count_day_report';
            $report->value = $newArray;
            $report->save();
        }
        $this->info('收入日报表');

    }
}
